﻿#include "hsplayer.h"

HSPlayer::HSPlayer()
{
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground, true);

    this->setStyleSheet("background-color: rgb(0,0,0);\
                        color: rgb(255,255,255);\
                        font: 20pt \"Sans Serif\";");


    this->setAlignment(Qt::AlignRight | Qt::AlignBottom);
    HS_Player = new HSFFmpeg();

}


HSPlayer::~HSPlayer()
{

}

void  HSPlayer::HSPlayer_Show(){
    this->hide();
    sleep_ms(50);
    this->show();
}
void   HSPlayer::HSPlayer_Resize(int w,int h){
    this->resize(w,h);
    emit HS_PlayControl_PlayStatusChange(HSPlayer_Stop);
    HS_Player->HS_SDL2_Deinit();
    HS_Player->HS_FFmpeg_Set_WindowRect(this->x(),this->y(),w,h);
    HS_SDL2_HAS_Init = false;
    if(HS_FFMPEG_HAS_Init)
        HS_Player->HS_FFmpeg_ReSws();
    this->HSPlayer_Start();
}

void   HSPlayer::HSPlayer_Move(int x,int y){
    this->move(x,y);
    HS_Player->HS_FFmpeg_Set_WindowRect(this->x(),this->y(),this->width(),this->height());
}

void   HSPlayer::HSPlayer_Set_ChannelText(QString ch){
     this->text().clear();
     this->setText(ch);
}
void   HSPlayer::HSPlayer_Start(){

    if(HS_SDL2_HAS_Init==false)
        HS_SDL2_HAS_Init = HS_Player->HS_SDL2_Init();

    if(HS_FFMPEG_HAS_Init==false)
        HS_FFMPEG_HAS_Init = HS_Player->HS_FFmpeg_Init();
    if(HS_SDL2_HAS_Init && HS_FFMPEG_HAS_Init){
        HS_Player->start();
        for(int i=0;i<10;i++){
            sleep_ms(200);
            if(HS_Player->HS_Control != nullptr){
                 connect(this,SIGNAL(HS_PlayControl_PlayStatusChange(int)),HS_Player->HS_Control,SLOT(HS_PlayControl_PlayStatusChange(int)),Qt::QueuedConnection);
                 connect(this,SIGNAL(HS_PlayControl_PlaySpeedChange(int,int,int)),HS_Player->HS_Control,SLOT(HS_PlayControl_PlaySpeedChange(int,int,int)),Qt::QueuedConnection);

                 break;
            }
            if(i==0){
                PRTLOG()<<"Create HSPlayer fail";
                delete HS_Player;
            }
        }
    }else if (!HS_FFMPEG_HAS_Init)PRTLOG()<<"[HSPlayer]:FFmpeg init fail";
    else if (!HS_SDL2_HAS_Init)PRTLOG()<<"[HSPlayer]:SDL2 init fail";
    emit HS_PlayControl_PlayStatusChange(HSPlayer_Play);
    HSPlayer_Show();
}

void   HSPlayer::HSPlayer_Local_LoadFile(QString videoPath){
    HS_StreamURL = videoPath;
    this->HS_Player->HS_FFmpeg_SetUrl(HS_RECORDING,videoPath);
}


void HSPlayer::mousePressEvent(QMouseEvent* event)
{
    if((event->type() == QEvent::MouseButtonDblClick)&&(event->buttons() == Qt::LeftButton))
    {
        if(HS_IsFullScreen){

        }else{

        }

    }else{
        return QLabel::mousePressEvent(event);
    }
}
